package mid.股票问题.买卖股票的最佳时机含手续费;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 买卖股票的最佳时机含手续费,实在想不到，到时候看看动态规划
 * @date 2022/11/26 15:02
 */
public class Demo714 {
    public static void main(String[] args) {
        int[] nums={1,7,5,9};
        int i = maxProfit(nums, 2);
        System.out.println(i);
    }
    public static int maxProfit(int[] prices, int fee) {
        int buy = prices[0] + fee;
        int sum = 0;
        for (int p : prices) {
            if (p + fee < buy) {
                //更新买入的低点
                buy = p + fee;
            } else if (p > buy){
                //计算卖出赚的总和
                sum += p - buy;
                //更新buy,后面有更大的数字，就累加
                buy = p;
            }
        }
        return sum;
    }
}
